package student_system.Controller;

import student_system.Service.IUserService;
import student_system.Service.UserServiceImpl;
import student_system.bean.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
    public RegisterServlet() {
        super();
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        super.doGet(req, resp);
        //获取前台页面输入的数据
        String username = request.getParameter("username");
        String pass = request.getParameter("password");
        String message="";//提示信息
        User user = new User(username,pass);//实例化注册对象，并传入参数
        IUserService userService = new UserServiceImpl();
        HttpSession session=request.getSession();//创建一个session会话，获取当前会话
        //判断注册方法是否返回结果是否为true
        if(userService.registerUser(user)) {
            message = "注册成功！";
            session.setAttribute("message", message);//设置提示信息内容
            response.sendRedirect("message.jsp");//重定向至信息提示页面
        }else {
            message = "注册失败,请重新注册！";
            session.setAttribute("message", message);
            response.sendRedirect("message.jsp");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
